# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):
    def isSymmetric(self, root):
        """
        :type root: TreeNode
        :rtype: bool
        """
        if not root or (not root.left and not root.right):
            return True
        if (root.left and not root.right) or (not root.left and root.right):
            return False
        lq = [root.left]
        rq = [root.right]
        while lq and rq:
            l_node = lq.pop(0)
            r_node = rq.pop(0)
            if not l_node and not r_node:
                continue
            elif (not l_node and r_node) or (l_node and not r_node):
                return False
            if l_node.val != r_node.val:
                return False
            lq += [l_node.left, l_node.right]
            rq += [r_node.right, r_node.left]
        if lq or rq:
            return False
        return True
